﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace DisplayBoardInfo.Timer
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class Timer : UserControl
    {
        DateTime minutesLeft;

        public Timer()
        {
            int timeExpected = 15;
            InitializeComponent();
            minutesLeft = new DateTime(1, 1, 1, 1, timeExpected, 0);
            UpdateBoard();
            
            var timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
            AnalogView = false;
        }

        private bool _analogView;

        public bool AnalogView
        {
            get { return _analogView; }
            set { _analogView = value; ChangeView(value); }
        }


        private void UpdateBoard()
        {
            UpdateLabelCountDown();
            UpdateLabelTime();
            UpdateClock();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            minutesLeft = minutesLeft.AddSeconds(-1);
            UpdateBoard();
        }

        private void UpdateLabelCountDown()
        {
            lblCountDown.Content = string.Format("Do końca wizyty: {0}", minutesLeft.ToString("mm:ss"));
        }

        private void UpdateLabelTime()
        {
            lblCurrentTime.Content = DateTime.Now.ToString("HH:mm:ss");
        }

        private void UpdateClock()
        {
            secondHand.Angle = DateTime.Now.Second * 6;
            minuteHand.Angle = DateTime.Now.Minute * 6 + DateTime.Now.Second * 0.1;
            hourHand.Angle = DateTime.Now.Hour * 30 + DateTime.Now.Minute * 0.5;
        }

        private void ChangeView(bool analog)
        {
            if (analog)
            {
                gridDigital.Visibility = System.Windows.Visibility.Hidden;
                gridAnalog.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                gridDigital.Visibility = System.Windows.Visibility.Visible;
                gridAnalog.Visibility = System.Windows.Visibility.Hidden;
            }
        }
    }
}
